<script setup>
import {getArticleList, delArticle} from '@/api/article.js'
import {ref} from 'vue'
import {
  Edit,
  Delete
} from '@element-plus/icons-vue'
import SelectBox from './components/SelectBox.vue'
import Drawer from './components/Drawer.vue'
import {formatTime} from '@/utils/farmat.js'
// 定义请求参数对象
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
const loading = ref(false)
const list = ref([]) // 文章列表
const total = ref(0) // 条数
const getList = async () => {
  loading.value = true
  const res = await getArticleList(params.value)
  list.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getList()

//分页相关
const handleSizeChange = (val) => {
  // 只要每页条数变化了，那么原本正在访问的当前页意义就不大了，所以重新从第一页渲染接口
  params.value.pagenum = 1
  params.value.pagesize = val
  getList()
}
const handleCurrentChange = (val) => {
  params.value.pagenum = val
  getList()
}

// 搜索和重置
const searchBtn = () => {
  // 搜索逻辑：按照最新的条件，重新请求第一页数据
  params.value.pagenum = 1
  getList()
}

const reSetBtn = () => {
  // 重置逻辑：将筛选条件清空，重新请求第一页面数据
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getList()
}

const delBtn = (id) => {
  ElMessageBox.confirm(
      '确认删除吗?',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        await delArticle(id)
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        getList()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '删除失败',
        })
      })
}

// 抽屉相关
const drawerRef = ref()
const addBtn = () => {
  drawerRef.value.open({})
}
const editBtn = (row) => {
  drawerRef.value.open(row)
}
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加，最好渲染最后一页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    // 更新成最大页码，在渲染
    params.value.pagenum = lastPage
  }
  // 如果是编辑，直接渲染当前页
  getList()
}
</script>

<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="addBtn">发布文章</el-button>
    </template>
    <!--    表单区域-->
    <el-form inline>
      <el-form-item label="文章分类">
        <SelectBox v-model="params.cate_id"></SelectBox>
      </el-form-item>
      <el-form-item label="发布状态">
        <el-select v-model="params.state">
          <!--后台标记发布状态就是通过中文标记的，已发布/草稿-->
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchBtn">搜索</el-button>
        <el-button @click="reSetBtn">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域-->
    <el-table v-loading="loading" :data="list" style="width: 100%">
      <el-table-column prop="title" label="文章标题">
        <template #default="{row}">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="cate_name" label="名称"></el-table-column>
      <el-table-column prop="pub_date" label="发表时间">
        <!-- 组件内有默认插槽，优先使用插槽内的数据，表格里的插槽要拿到row去渲染-->
        <template #default="{row}">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态"/>
      <el-table-column label="操作" width="100">
        <template #default="{row}">
          <el-button @click="editBtn(row)" size="small" type="primary" :icon="Edit" circle></el-button>
          <el-button @click="delBtn(row.id)" size="small" type="danger" :icon="Delete" circle></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="暂无数据"/>
      </template>
    </el-table>
    <!--分页-->
    <el-pagination
        v-model:current-page="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[2, 5, 10]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px; justify-content: flex-end"
    />
    <!--抽屉-->
    <Drawer ref="drawerRef" @success="onSuccess"></Drawer>
  </page-container>

</template>

<style lang="scss" scoped>

</style>

